package com.cheetah.protocol;

/**
 * @Author Administrator
 * @Date 2019/7/29
 * @Version V1.0
 * @Description:
 **/
public class ResponseBuilder<T> {

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder<T> {
        private boolean success;

        private int errorCode;

        private String errorMsg;

        private T data;

        public Builder success(boolean success) {
            this.success = success;
            return this;
        }

        public Builder errorCode(int errorCode) {
            this.errorCode = errorCode;
            return this;
        }

        public Builder errorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
            return this;
        }


        public Builder data(T t) {
            this.data = t;
            return this;
        }

        public Response<T> build() {
            Response<T> response = new Response();
            response.setSuccess(success);
            response.setErrorCode(errorCode);
            response.setErrorMsg(errorMsg);
            response.setData(data);
            return response;
        }

        public Response<T> success(T t) {
            return builder().errorCode(ErrorCode.ERROR_CODE_0)
                    .success(true)
                    .data(t)
                    .build();
        }

        public Response<T> fail(int errorCode,String errorMsg) {
            return builder().errorCode(errorCode)
                    .success(false)
                    .errorMsg(errorMsg)
                    .build();
        }
    }
}
